<?php     
    header("Content-type: text/html; charset=utf-8;");
    ini_set('memory_limit', '-1');
    require_once('../../../cfg/config.inc');
    require_once( '../../../ppf/pdo_mysql.php'); 
    require_once('./excel/PHPExcel.php');
	require_once('./excel/PHPExcel/IOFactory.php');
    require_once('./excel/PHPExcel/Reader/Excel5.php');
	require_once('./excel/PHPExcel/Writer/Excel5.php');
	function p($res){#打印结果
		echo "<pre>";
		print_r($res);
		echo "</pre>";	
	}
	$pd=new pdo_mysql();
		//导出Excel
		$excel = new PHPExcel();
		//Excel表格式
		$letter = array('A','B','C','D','E','F','G','H','I');
		//表头数组
		$tableheader = array('编号','学校','姓名','性别','语文','数学','英语','三门总分','班级');
		//填充表头信息
		for($i = 0;$i < count($tableheader);$i++) {
			$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
		}
		$sql="SELECT * FROM class_placement_stu ORDER BY class ASC";
		$attr = $pd->query($sql)->fetchAll(PDO::FETCH_ASSOC);
		if($attr){
			//填充表格信息
			for ($i = 2;$i <= count($attr) + 1;$i++) {
				$j = 0;
				foreach ($attr[$i - 2] as $key=>$value) {
					$excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
					$j++;
				}
			}
			//创建Excel输入对象
			$write = new PHPExcel_Writer_Excel5($excel);
			header("Pragma: public");
			header("Expires: 0");
			header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
			header("Content-Type:application/force-download");
			header("Content-Type:application/vnd.ms-execl");
			header("Content-Type:application/octet-stream");
			header("Content-Type:application/download");;
			header("Content-Disposition:attachment;filename='stu.xls'");
			header("Content-Transfer-Encoding:binary");
			$write->save('php://output');
		}else{
			echo "<script>window.alert('请先导入数据!')</script>";
		}
		

	$pd->close();
	unset($pd);
?>